<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Laomo 的后院</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="一位藕粉，Go Python 程序员">
<meta property="og:type" content="website">
<meta property="og:title" content="Laomo 的后院">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Laomo 的后院">
<meta property="og:description" content="一位藕粉，Go Python 程序员">
<meta property="article:author" content="Laomo.">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Laomo 的后院" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Laomo 的后院</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-【LaTeX测试】一些著名的公式" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/05/%E3%80%90LaTeX%E6%B5%8B%E8%AF%95%E3%80%91%E4%B8%80%E4%BA%9B%E8%91%97%E5%90%8D%E7%9A%84%E5%85%AC%E5%BC%8F/" class="article-date">
  <time datetime="2020-04-05T15:44:24.000Z" itemprop="datePublished">2020-04-05</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/05/%E3%80%90LaTeX%E6%B5%8B%E8%AF%95%E3%80%91%E4%B8%80%E4%BA%9B%E8%91%97%E5%90%8D%E7%9A%84%E5%85%AC%E5%BC%8F/">【LaTeX测试】一些著名的公式</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><strong><em>普朗克黑体辐射公式</em></strong><br>$$<br>\mathbf{I}(v, T) = \frac{2hv^3}{c^2}\frac{1}{e^{hv/kT} - 1}<br>$$</p>
<p><strong><em>万有引力内部公式</em></strong><br>$$<br>F(a) = \frac{GMm}{R^2}(\frac{1-(1-a^2)^{1.5}}{a^2})<br>$$<br><strong><em>毕达哥拉斯定理</em></strong><br>$$<br>a^2 + b^2 = c^2<br>$$<br><strong><em>欧拉公式</em></strong><br>$$<br>e^{i\pi} + 1 = 0<br>$$<br><strong><em>薛定谔方程(某些符号显示不出来)</em></strong><br>$$<br>ih\frac{\partial}{\partial t}\Psi(r, t) = H\Psi(r, t)<br>$$</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/05/%E3%80%90LaTeX%E6%B5%8B%E8%AF%95%E3%80%91%E4%B8%80%E4%BA%9B%E8%91%97%E5%90%8D%E7%9A%84%E5%85%AC%E5%BC%8F/" data-id="ck8swb2zk0003wds6gw0q0pjk" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%8A%B1%E9%87%8C%E8%83%A1%E5%93%A8/" rel="tag">花里胡哨</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-搞定Python装饰器（三）实现property类" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%89%EF%BC%89%E5%AE%9E%E7%8E%B0property%E7%B1%BB/" class="article-date">
  <time datetime="2020-04-04T04:06:24.000Z" itemprop="datePublished">2020-04-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%89%EF%BC%89%E5%AE%9E%E7%8E%B0property%E7%B1%BB/">搞定Python装饰器（三）实现property类</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>上一章，我们学习了如何编写一个对象型装饰器，顺便也了解了一下如何编写一个类对象装饰器，本章我们将通过实现一个property类，来了解类对象装饰器是如何工作的。</p>
<p><em>(阅读本章前，建议先去了解一下property的使用方式)</em></p>
<h2 id="property的原理"><a href="#property的原理" class="headerlink" title="property的原理"></a>property的原理</h2><p>在着手编写之前，先来了解一下property的大致原理。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">property(fget=<span class="literal">None</span>, fset=<span class="literal">None</span>, fdel=<span class="literal">None</span>, doc=<span class="literal">None</span>)</span><br></pre></td></tr></table></figure>

<p>实例化一个property的参数都是可选的。我们不难发现，第一个参数就是<code>fget</code>，也就是<code>getter</code>，将getter作为第一个参数的巧妙之处就在于使用<code>@property</code>时，语法糖会被转换成<code>name = property(name)</code>，而name就是getter方法，所以说<code>name</code>就会成为一个带有<code>getter</code>的<code>property</code>对象，而<code>getter</code>方法就是name方法。<em>可能有点绕，大家可以结合代码加以理解</em>。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>@property</span><br><span class="line"><span class="meta">... </span><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">pass</span></span><br><span class="line">...</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>foo</span><br><span class="line">&lt;property object at <span class="number">0x7b755b97c0</span>&gt;</span><br></pre></td></tr></table></figure>

<h2 id="编写property类"><a href="#编写property类" class="headerlink" title="编写property类"></a>编写property类</h2><p>了解大致原理之后，我们开始实现我们的property类，为了不与python内置的property冲突，我们的property类名称将首字母大写。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Property</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, fget=None, fset=None, fdel=None)</span>:</span></span><br><span class="line">        self.fget = fget</span><br><span class="line">        self.fset = fset</span><br><span class="line">        self.fdel = fdel</span><br></pre></td></tr></table></figure>
<p>初始化函数部分就不多说了，接下来我们来实现 <code>__get__</code>, <code>__set__</code>, <code>__del__</code>方法。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Property</span>:</span></span><br><span class="line">    <span class="comment"># ...</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__get__</span><span class="params">(self, obj, *args, **kwargs)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.fget:</span><br><span class="line">            <span class="keyword">raise</span> AttributeError(<span class="string">'unreadable attribute.'</span>)</span><br><span class="line">        <span class="keyword">return</span> self.fget(obj)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__set__</span><span class="params">(self, obj, value, *args, **kwargs)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.fset:</span><br><span class="line">            <span class="keyword">raise</span> AttributeError(<span class="string">'can\'t set attribute'</span>)</span><br><span class="line">        self.fset(obj, value)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__del__</span><span class="params">(self, *args, **kwargs)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.fdel:</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        self.fdel()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># ...</span></span><br></pre></td></tr></table></figure>

<p>因为property对象不需要被调用，所以说就不需要实现<code>__call__</code>方法，所以说装饰器的返回类型由装饰器决定。</p>
<p>实现了这三个方法，就可以作为一个描述符使用了，但是我们在使用property装饰器的时候，传入的只有<code>getter</code>，<code>setter</code>和<code>deleter</code>还没有传入，要设置他们，我们仍然使用装饰器。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Property</span>:</span></span><br><span class="line">    <span class="comment"># ...</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">setter</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        self.fset = func</span><br><span class="line">        <span class="keyword">return</span> func</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">getter</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        self.fget = func</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> func</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">deleter</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        self.fdel = func</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> func</span><br></pre></td></tr></table></figure>

<p>这几个方法只是用来设置自身的属性，不影响被装饰的函数，所以我们要返回原来的函数。</p>
<p>至此，我们的Property类就写完了，接下来是完整的源码。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Property</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, fget=None, fset=None, fdel=None)</span>:</span></span><br><span class="line">        self.fget = fget</span><br><span class="line">        self.fset = fset</span><br><span class="line">        self.fdel = fdel</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__get__</span><span class="params">(self, obj, *args, **kwargs)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.fget:</span><br><span class="line">            <span class="keyword">raise</span> AttributeError(<span class="string">'unreadable attribute.'</span>)</span><br><span class="line">        <span class="keyword">return</span> self.fget(obj)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__set__</span><span class="params">(self, obj, value, *args, **kwargs)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.fset:</span><br><span class="line">            <span class="keyword">raise</span> AttributeError(<span class="string">'can\'t set attribute'</span>)</span><br><span class="line">        self.fset(obj, value)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__del__</span><span class="params">(self, *args, **kwargs)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.fdel:</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        self.fdel()</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">setter</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        self.fset = func</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> func</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">getter</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        self.fget = func</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> func</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">deleter</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        self.fdel = func</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> func</span><br></pre></td></tr></table></figure>

<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><p>接着我们来测试一下我们的Property类</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># test_prop.py</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> prop2 <span class="keyword">import</span> Property</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Human</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, name)</span>:</span></span><br><span class="line">        self.__name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @Property</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">name</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> self.__name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">set_name</span><span class="params">(self, name)</span>:</span></span><br><span class="line">        self.__name = name</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    human = Human(<span class="string">'Nezha'</span>)</span><br><span class="line">    print(human.name)</span><br><span class="line"></span><br><span class="line">    human.name = <span class="string">'Zhaer'</span></span><br><span class="line">    print(human.name)</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ python test_prop.py</span><br><span class="line">Nezha</span><br><span class="line">Zhaer</span><br></pre></td></tr></table></figure>

<p>至此，我们的Property就大功告成啦！</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>经过了三章的讲解，相信你已经对装饰器有了自己的理解。</p>
<p>装饰器是python中提高编程效率的主要手段。若装饰器用得好，编程效率将会大大提高。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%89%EF%BC%89%E5%AE%9E%E7%8E%B0property%E7%B1%BB/" data-id="ck8swb30j000bwds68zclb575" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python%E6%95%99%E7%A8%8B/" rel="tag">python教程</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-搞定Python装饰器（二）" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%BA%8C%EF%BC%89/" class="article-date">
  <time datetime="2020-04-04T01:25:17.000Z" itemprop="datePublished">2020-04-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%BA%8C%EF%BC%89/">搞定Python装饰器（二）</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="对象型装饰器"><a href="#对象型装饰器" class="headerlink" title="对象型装饰器"></a>对象型装饰器</h2><p>上一篇文章讲了如何编写一个函数型装饰器，这篇文章我们来讲讲如何编写一个类或者对象型的装饰器。</p>
<p>之前说过，装饰器要是一个可调用对象，所以说当我们想用对象作为一个装饰器时，只需要实现<code>__call__</code>方法即可。</p>
<p>接下来，我们来修改timeit，实现一类两用。timeit本质上是一个timer，timer可以作为计时器使用，但timer对象作为装饰器使用时，可以为函数进行计时。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># mytimer.py</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Timer</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.__start = <span class="number">0</span></span><br><span class="line">        self.__last = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">        self.__started = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">start</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.__started:</span><br><span class="line">            self.__start = time.time()</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">raise</span> Exception(<span class="string">'Timer has been started'</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">stop</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> self.__started:</span><br><span class="line">            self.last = self.__start - time.time()</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">raise</span> Exception(<span class="string">'Timer is not running.'</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">last_time</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.__started:</span><br><span class="line">            <span class="keyword">return</span> self.__last</span><br><span class="line">        <span class="keyword">return</span> time.time() - self.__start</span><br></pre></td></tr></table></figure>
<p>这是一个简单计时器的基本逻辑，现在我们来实现<code>__call__</code>方法。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Timer</span>:</span></span><br><span class="line">    <span class="comment"># ....</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__call__</span><span class="params">(self, func)</span>:</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(*args, **kwargs)</span>:</span></span><br><span class="line">            start = time.time() * <span class="number">1000</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">                rtn = func(*args, **kwargs)</span><br><span class="line">            <span class="keyword">except</span>:</span><br><span class="line">                <span class="keyword">raise</span></span><br><span class="line">            <span class="keyword">finally</span>:</span><br><span class="line">                print(<span class="string">'[ time = %s ms ]'</span> %</span><br><span class="line">                        (time.time() * <span class="number">1000</span> - start))</span><br><span class="line">            <span class="keyword">return</span> rtn</span><br><span class="line">        <span class="keyword">return</span> wrapper</span><br><span class="line">    <span class="comment"># ....</span></span><br></pre></td></tr></table></figure>

<p>由于实现了<code>__call__</code>方法，我们的对象可以像函数一样被调用了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># test_obj.py</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> mytimer <span class="keyword">import</span> Timer</span><br><span class="line"></span><br><span class="line">timeit = Timer()</span><br><span class="line"></span><br><span class="line"><span class="meta">@timeit</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5013</span>):</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># foo = timeit(foo)</span></span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ python test_obj.py</span><br><span class="line">[ time &#x3D; 1.08 ms ]</span><br></pre></td></tr></table></figure>

<h2 id="类对象装饰器"><a href="#类对象装饰器" class="headerlink" title="类对象装饰器"></a>类对象装饰器</h2><p>python中，类也可以作为装饰器使用，由于object类的元类(metaclass)已经帮我们实现了<code>__call__</code>方法，所以我们只需要在我们类中的<code>__call__</code>方法实现中编写一层函数即可，即<code>__call__</code>方法就是装饰函数。</p>
<p>对于<code>type.__call__(cls, *argv, **kwargs)</code>，除了<code>cls</code>参数以外，其余的参数都会随<code>object.__new__</code>返回的实例化对象一齐传入到相应的<code>__init__</code>方法中，所以我们只需要在<code>__init__</code>方法中处理被装饰函数即可。对应类对象这时候就成为了被装饰函数替代者。</p>
<p>下一章，我们将实现一个经典的类对象装饰器————<strong>property</strong></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%BA%8C%EF%BC%89/" data-id="ck8swb30n000dwds6ebq4fyux" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python%E6%95%99%E7%A8%8B/" rel="tag">python教程</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-【图片测试】可爱的吒儿" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/04/%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF/" class="article-date">
  <time datetime="2020-04-04T01:00:00.000Z" itemprop="datePublished">2020-04-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/04/%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF/">【图片测试】可爱的吒儿</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="分享一些可爱的吒儿的图片"><a href="#分享一些可爱的吒儿的图片" class="headerlink" title="分享一些可爱的吒儿的图片"></a>分享一些可爱的吒儿的图片</h2><h3 id="电影截图"><a href="#电影截图" class="headerlink" title="电影截图"></a>电影截图</h3><p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Ch_zd.jpg" alt="h_zd"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Ch_cry.jpg" alt="h_cry"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Ch_s.jpg" alt="h_s"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Ch_hruA.jpg" alt="h_hruA"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Ch_hruB.jpg" alt="h_hruB"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cx_hns.jpg" alt="x_hns"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cs_q.jpg" alt="s_q"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cs_qn.jpg" alt="s_qn"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cs_qs.jpg" alt="s_qs"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cs_n.jpg" alt="s_n"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cs_wtf.jpg" alt="h_wtf"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cs_wow.jpg" alt="s_wow"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cx_ahhh.jpg" alt="x_ahhh"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cx_hpy.jpg" alt="x_hpy"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Csc_dncry.jpg" alt="sc_dncry"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Csc_t.jpg" alt="sc_t"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cq_map.jpg" alt="q_map"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cqz_ucant.jpg" alt="qz_ucant"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cqz_seu.jpg" alt="qz_seu"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cqz_dfg.jpg" alt="qz_dfg"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cqz_g.jpg" alt="qz_g"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cf_b.jpg" alt="f_b"></p>
<p><img src="%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF%5Cf_slp.jpg" alt="f_slp"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/04/%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF/" data-id="ck8swb2zq0004wds636dhdhe8" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%93%AA%E5%90%92%E7%9B%B8%E5%85%B3/" rel="tag">哪吒相关</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-搞定Python装饰器（一）" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/03/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%80%EF%BC%89/" class="article-date">
  <time datetime="2020-04-03T15:55:00.000Z" itemprop="datePublished">2020-04-03</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/03/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%80%EF%BC%89/">搞定 Python 装饰器（一）</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>来聊一聊 Python 中的一个很有意思的特性——<strong>装饰器</strong></p>
<blockquote>
<p>Python 装饰器，实际上是一个用于接收被装饰函数的一个可调用对象。</p>
</blockquote>
<p>为什么说是可调用对象呢，因为在Python中，任何一个实现了<code>__call__</code>方法的对象都能被作为可调用对象，所以，装饰器可以是<strong>类</strong>，<strong>对象</strong>或者函数。</p>
<p>回到正题，装饰器实际上是Python的一个语法糖，当我们使用一个装饰器的时候，实际上在编译期间会被编译成为一个赋值语句。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@timeit</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5013</span>):</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 编译后：</span></span><br><span class="line"><span class="comment"># foo = timeit(foo)</span></span><br></pre></td></tr></table></figure>

<p>所以说，我们在 foo 上面盖上一个修饰器，实际上和调用这个装饰器传入这个函数再赋值给这个函数，没啥子区别。不信？来看看字节码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># @timeit</span></span><br><span class="line"><span class="comment"># def foo():</span></span><br><span class="line"><span class="comment">#     pass</span></span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dis.dis(<span class="string">'@timeit\ndef foo():pass'</span>)</span><br><span class="line">  <span class="number">1</span>           <span class="number">0</span> LOAD_NAME                <span class="number">0</span> (timeit <span class="comment"># 加载 timeit 对象</span></span><br><span class="line"></span><br><span class="line">  <span class="number">2</span>           <span class="number">2</span> LOAD_CONST               <span class="number">0</span> (&lt;code object foo at <span class="number">0x0000021C895F7450</span>, file <span class="string">"&lt;dis&gt;"</span>, line <span class="number">1</span>&gt;)</span><br><span class="line">              <span class="number">4</span> LOAD_CONST               <span class="number">1</span> (<span class="string">'foo'</span>)</span><br><span class="line">              <span class="number">6</span> MAKE_FUNCTION            <span class="number">0</span>	      </span><br><span class="line">              <span class="number">8</span> CALL_FUNCTION            <span class="number">1</span>        <span class="comment"># 调用 timeit 方法</span></span><br><span class="line">             <span class="number">10</span> STORE_NAME               <span class="number">1</span> (foo)  <span class="comment"># 重新给 foo 赋值</span></span><br><span class="line">             <span class="number">12</span> LOAD_CONST               <span class="number">2</span> (<span class="literal">None</span>)</span><br></pre></td></tr></table></figure>



<h2 id="如何编写一个装饰器"><a href="#如何编写一个装饰器" class="headerlink" title="如何编写一个装饰器"></a>如何编写一个装饰器</h2><p>我们已经知道了，装饰器就是一个<strong>返回一个函数</strong>的可调用对象。既然知道了原理，写起来就很简单了。</p>
<p>接下来，我们就一步一步实现一个程序计时器——<code>timeit</code></p>
<p>首先，装饰器是一个可调用对象，一般对于可调用对象我们都会选择使用函数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">timeit</span><span class="params">(func)</span>:</span></span><br><span class="line">    <span class="comment"># Do something...</span></span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>

<p>装饰器不仅要可以调用，还需要返回一个可调用对象，这个可调用对象一般拥有下面几个特征：</p>
<ol>
<li>返回和被装饰函数相同（或者同类型）的返回值</li>
<li>形式参数尽可能符合被修饰的函数的形式参数</li>
<li>在调用被装饰函数的基础上，做一些别的事情</li>
</ol>
<p>这个可调用对象有一点特殊，需要访问到外面那层函数的域，以获得被装饰的函数并调用。对于这种情况，我们会选择使用闭包。</p>
<p>在timeit里面编写闭包函数，该函数的形参应该是与被装饰函数相同，但是，每个函数的形参可能都不一样，所以对于这种不确定参数，我们需要用<strong>变参</strong>，python 中的变参表示有 <code>*</code>和<code>**</code>两种表示，分别表示可变位置参数和可选关键字参数。</p>
<p><strong><em>这个闭包，就是装饰器本体，timeit 函数只不过是一个壳，用于释放装饰函数。</em></strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">timeit</span><span class="params">(func)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(*args, **kwargs)</span>:</span></span><br><span class="line">    	<span class="comment"># Do something...</span></span><br><span class="line">    	<span class="keyword">pass</span></span><br><span class="line">    <span class="keyword">return</span> wrapper <span class="comment"># 返回（释放）装饰器</span></span><br></pre></td></tr></table></figure>

<p><strong><em>我们调用被装饰过后的函数，实际上是调用释放出来的函数</em></strong></p>
<p>接下来，我们只要编写计时器函数的逻辑即可</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># mytimer.py</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">timeit</span><span class="params">(func)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(*args, **kwargs)</span>:</span></span><br><span class="line">        start = time.time() * <span class="number">1000</span> <span class="comment">#转换成毫秒</span></span><br><span class="line">        </span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            rtn = func()</span><br><span class="line">        <span class="keyword">except</span>:</span><br><span class="line">            <span class="keyword">raise</span></span><br><span class="line">        <span class="keyword">finally</span>:</span><br><span class="line">            print(<span class="string">'[ time = %s ms ]'</span> % (time.time() * <span class="number">1000</span> - start))</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> rtn</span><br><span class="line">	<span class="keyword">return</span> wrapper</span><br></pre></td></tr></table></figure>

<p>好了，我们的timeit装饰器搞定了，来试试吧</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># test.py</span></span><br><span class="line"><span class="keyword">from</span> mytimer <span class="keyword">import</span> timeit</span><br><span class="line"></span><br><span class="line"><span class="meta">@timeit</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5013</span>):</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    foo()</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ python test.py</span><br><span class="line">[ time &#x3D; 1.00048828125 ms ]</span><br></pre></td></tr></table></figure>

<p>看起来运行得不错，那么接下来，我们将改进 timeit，让其能够选择是以毫秒输出还是以秒输出。</p>
<h2 id="如何编写有参装饰器"><a href="#如何编写有参装饰器" class="headerlink" title="如何编写有参装饰器"></a>如何编写有参装饰器</h2><p>接下来教大伙一个很简单的方法来还原装饰器语法糖，屡试不爽。</p>
<p>对于这样的装饰器使用，会被转换成下面的样子</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@timeit(True)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">f</span><span class="params">()</span>:</span></span><br><span class="line">	<span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 会被转换成下面的样子</span></span><br><span class="line"><span class="comment"># f = timeit(True)(f)</span></span><br></pre></td></tr></table></figure>

<p>为了将装饰器语法糖转换成赋值语句，可以使用下面那个公式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">被装饰函数 &#x3D; 装饰器声明 ( 被装饰函数 )</span><br></pre></td></tr></table></figure>
<p><em>装饰器声明，即‘@’右侧的内容</em></p>
<p>根据转换出来的<code>f = timeit(True)(f)</code>可以知道，我们需要一个接受一个<code>bool</code>类型的可调用对象，在这个对象被调用后，返回另一个可调用对象，这个对象接受传入的<code>f</code>，最后再返回装饰器本体。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">timeit</span><span class="params">(use_ms)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(func)</span>:</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">inner_wrapper</span><span class="params">(*args, **kwargs)</span>:</span></span><br><span class="line">            <span class="comment"># Do something...</span></span><br><span class="line">            <span class="keyword">pass</span></span><br><span class="line">        <span class="keyword">return</span> inner_wrapper</span><br><span class="line">    <span class="keyword">return</span> wrapper</span><br></pre></td></tr></table></figure>

<p>现在，我们可以修改我们的 <code>mytimer.py</code>，让 timeit 装饰器接受一个<code>bool</code>参数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># mytimer.py</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">timeit</span><span class="params">(use_ms :bool)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(func)</span>:</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">inner_wrapper</span><span class="params">(*args, **kwargs)</span>:</span></span><br><span class="line">            p = <span class="number">1000</span> <span class="keyword">if</span> use_ms <span class="keyword">else</span> <span class="number">1</span></span><br><span class="line">            s = <span class="string">'ms'</span> <span class="keyword">if</span> use_ms <span class="keyword">else</span> <span class="string">'s'</span></span><br><span class="line">            start = time.time() * p</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">                rtn = func()</span><br><span class="line">            <span class="keyword">except</span>:</span><br><span class="line">                <span class="keyword">raise</span></span><br><span class="line">            <span class="keyword">finally</span>:</span><br><span class="line">                print(<span class="string">'[ time = %s %s ]'</span> % (time.time() * p - start, s))</span><br><span class="line">                      </span><br><span class="line">            <span class="keyword">return</span> rtn</span><br><span class="line">        <span class="keyword">return</span> inner_wrapper</span><br><span class="line">    <span class="keyword">return</span> wrapper</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># test_t.py</span></span><br><span class="line"><span class="keyword">from</span> mytimer <span class="keyword">import</span> timeit</span><br><span class="line"></span><br><span class="line"><span class="meta">@timeit(True)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5013</span>):</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    foo()</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># test_f.py</span></span><br><span class="line"><span class="keyword">from</span> mytimer <span class="keyword">import</span> timeit</span><br><span class="line"></span><br><span class="line"><span class="meta">@timeit(false)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">5013</span>):</span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    foo()</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ python test_t.py</span><br><span class="line">[ time &#x3D; 1.04 ms ]</span><br><span class="line"></span><br><span class="line">$ python test_f.py</span><br><span class="line">[ time &#x3D; 0.0014 s ]</span><br></pre></td></tr></table></figure>

<p>其他地方，就不多赘述了。</p>
<p>下一篇文章讲一讲对象类型的装饰器</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/03/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%80%EF%BC%89/" data-id="ck8swb30f0009wds6ee589f4n" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python%E6%95%99%E7%A8%8B/" rel="tag">python教程</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-我把Termux整废了" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/03/%E6%88%91%E6%8A%8ATermux%E6%95%B4%E5%BA%9F%E4%BA%86/" class="article-date">
  <time datetime="2020-04-03T09:29:36.000Z" itemprop="datePublished">2020-04-03</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/03/%E6%88%91%E6%8A%8ATermux%E6%95%B4%E5%BA%9F%E4%BA%86/">我把Termux整废了</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>事情是这样的，我昨天想在手机上面双开Termux，用了好几种办法</p>
<ol>
<li>手机分身（MIUI）</li>
<li>进行软件分身App</li>
<li>VMOS</li>
</ol>
<p>第三种方法成功了，然后我便将第一第二种分身出来的Termux删除。</p>
<p>正是这个删除，导致附带的<code>libtermux.so</code>被删去，原Termux也报废了。</p>
<p><code>libtermux.so</code>应该是与data目录隔开的，两个分身公用一个so文件，这导致删除分身时，原Termux需要的<code>libtermux.so</code>也被删去。</p>
<p>由于手机没有root，无法读取Termux的data目录，我将近8G的数据，就这么没了…</p>
<p>所以，应用分身，并不是真的两个独立的App，彼此或许会共用某些数据，如果删除分身，那么原来App也会受到破坏。</p>
<blockquote>
<p>不作死就不会死</p>
</blockquote>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/03/%E6%88%91%E6%8A%8ATermux%E6%95%B4%E5%BA%9F%E4%BA%86/" data-id="ck8swb3080008wds61bpb415l" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%B5%81%E6%B0%B4%E8%B4%A6/" rel="tag">流水账</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-做藕粉的第236天" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/02/%E5%81%9A%E8%97%95%E7%B2%89%E7%9A%84%E7%AC%AC236%E5%A4%A9/" class="article-date">
  <time datetime="2020-04-01T16:50:03.000Z" itemprop="datePublished">2020-04-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/02/%E5%81%9A%E8%97%95%E7%B2%89%E7%9A%84%E7%AC%AC236%E5%A4%A9/">做藕粉的第236天</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="偶然的相遇"><a href="#偶然的相遇" class="headerlink" title="偶然的相遇"></a>偶然的相遇</h2><blockquote>
<p>距离2019年8月10日，已经236天了。</p>
</blockquote>
<p>8月10日我所经历的那件事，还历历在目。</p>
<p>8月9日，我和我爸从北京回到钦州，回来的路上，还去游了一下长沙。走在黄兴步行街上，我爸问我要不要去看电影，我想了想，还是算了吧，毕竟我当时也不太喜欢看电影，况且时间也挺晚了，走完步行街，我只想睡上一觉。</p>
<blockquote>
<p>我第一次错过了他</p>
</blockquote>
<p>8月10日，是我回到钦州的第二天，那天同以往一样，没什么不同的，只是我和舅舅已经7天没有痛快来上一碗螺蛳粉了，我便打电话约他。我顺利地约上了他并一起吃了粉，在我拿纸巾擦着嘴的时候，他和我说，要不要去看电影啊？我有两张IMAX优惠券哦，我下午没有安排，便答应了。（事实上绝大部分原因是因为IMAX）</p>
<p>看什么电影？<br>哪吒啊，最近那个哪吒不是挺火的吗？<br>好像是挺火的，那就去看这个吧。</p>
<p>实际上，我也不是第一次听到哪吒这部电影了，早在回钦州的路上，我好几次在推送看到过他，但一直没怎么留意，毕竟在浏览器下面推送的电影影评，我见过的，大多数都是标题刺激，内容乏味的破文章。</p>
<p>不过，虽然文章的标题不吸引我，配图却引起了我的注意。我记得其中一篇的配图是哪吒手拿着木锤子的那个画面。说实话，我有被吓到。我当时不断在将他和我当时认识的哪吒（正太版）进行特征匹配（有点像图像识别），结果大概匹配上了几个特征，<code>乾坤圈</code>和<code>头绳</code>，如果把这几点去掉，要是不告诉我我还真看不出来这是哪吒。</p>
<p>我掂量了一下这位哪吒，长得很有特色，看久了甚至有点<strong>可爱</strong>，还有点丧。这是我的第一印象，<strong>说实话，真挺可爱</strong>。</p>
<p>但是因为一些原因，我当时没有继续研究这个哪吒。</p>
<p>我俩到了电影院，新的手机又出毛病了，挺烦的，问我选那个位，我也就随便指了指中间靠后的位置。随后就是提前五分钟入场，坐在那里看广告。</p>
<p>进入IMAX影厅的感觉确实很爽，我站在巨幕下面，我能感受到自己多渺小。</p>
<p>电影开始了，我还是按以往那样，挺直腰，记要素，推剧情。</p>
<h2 id="墙头初见"><a href="#墙头初见" class="headerlink" title="墙头初见"></a>墙头初见</h2><p>虽然对于现在的我，开头的剧情已烂熟于心，但对于一刷的我来说，还是很有感觉的。作为一个技术宅，我首先看的一般不是剧情，而是画面，的确，这部电影的画面很棒。</p>
<p>哪吒的金色大字出现，伴随着唢呐的声音，鸡皮疙瘩都起来了。</p>
<p><strong>“我们还是早点回去，看能不能再为孩子做点啥子吧..”</strong></p>
<p>话刚说完，镜头转到了李府的院子，紧跟着的，是用富有磁性的烟嗓念出的打油诗</p>
<p><strong><em>关在府里无事干，<br>翻墙倒瓦摔瓶罐。<br>来来回回千百遍，<br>小爷也是很疲倦。</em></strong></p>
<p>这丧丧的烟嗓，加上使用<code>小爷</code>这样的词汇，<code>好了，我们的小哪吒要登场了</code>。<br>一个平滑的移镜从哪吒的身后移到侧身，我看到了他的脸。他的说话风格有点像青春期的少年，但他只有一岁。<strong>这哪吒，有点意思</strong>。<br>殷夫人问要不要踢毽子时，他的表情变化让我出现了一个推测<br><strong>他的性格将会很傲娇</strong></p>
<p>慢慢的，我开始了解他，对他的称呼逐渐从 “哪吒” 转变为 “吒儿”<br>我很同情吒儿，被村民误会的时候，我很难受，却什么也干不了。</p>
<p>在吒儿与敖丙在海边谈话的时候，我印证了我的猜想，他是一个<strong>傲娇小可爱</strong>，我逐渐喜欢上了这个哪吒，对他的外表则是越看越可爱。</p>
<p>看完电影，我只能用电影里李靖的一个台词形容我的心情</p>
<blockquote>
<p>“他是我儿”</p>
</blockquote>
<h2 id="被圈粉后"><a href="#被圈粉后" class="headerlink" title="被圈粉后"></a>被圈粉后</h2><p>看完电影出来，我意识到，我被吒儿圈粉了，当时脑子里全是这个小可爱。</p>
<p>果然，在当天，晚上我把哪吒的官方微博从头刷到底，我对他和这部电影有了更深入的认识。</p>
<h2 id="二刷"><a href="#二刷" class="headerlink" title="二刷"></a>二刷</h2><p>这是我第一次如此喜欢一个动漫人物，我经过了一个晚上的思想斗争后，我决定在8月15日再次到电影院看一遍这部电影，当时我也对我说，这可能是我最后一次去电影院看哪吒了，要珍惜这次机会。</p>
<p>买完票，顶着三十多度点烈日，脚踩这单车到了电影院。</p>
<p>吒儿真是个奇怪的角色，越看越可爱，说实话，我平时也喜欢那种傲娇又善良的女生。不知道为什么，吒儿怎么看都有种女孩子的感觉……</p>
<p>出了电影院，微博特别关注就来消息了，是破36亿票房的消息，当时我没想到，在145天后，这部电影将会破50亿票房。</p>
<h2 id="做一名事业粉"><a href="#做一名事业粉" class="headerlink" title="做一名事业粉"></a>做一名事业粉</h2><p>那天晚上，我逐渐开始关注哪吒的票房，看着实时票房不断增加的数据，很爽，逐渐的，我成为了一名事业粉。</p>
<p>2019年8月16日，全职高手和沉默的证人上映。</p>
<p>一大早，我看到哪吒被挤到了第三，我很担心，日冠是不是保不住了。到了下午，才发现只是虚惊一场，哪吒又回到了第一，看来还是不经打</p>
<p>做事业粉，明天都要定时收集数据，到了开学，我也不能每时每刻坐在电脑前面统计着数据。于是我写了个爬虫，定时爬取猫眼上的票房数据，那个程序由于哪吒的下映，已经用不了了（改改还能用），但仍然存在手机上<strong><em>(4-3更新：因为一次失误，导致termux报废，只有少数票房数据幸存😭)</em></strong>，作为哪吒事业粉的见证。</p>
<p>手机上现在还存有当时记录下来的数据，从9月1日到10月10日，后面由于一些原因，就没有记录了。</p>
<p>这些数据会一直存着，到了哪吒2上映，再拿出来做对比。</p>
<h2 id="哪吒电影事业粉群"><a href="#哪吒电影事业粉群" class="headerlink" title="哪吒电影事业粉群"></a>哪吒电影事业粉群</h2><p>中秋假期，我想了想，作为一个哪吒的粉丝，居然连粉丝群都没加，于是乎我在QQ上搜索哪吒，我记得有两个靠前的结果：“吒儿的生辰宴” 和 <strong>哪吒电影事业粉群</strong> ， 我作为一个事业粉，当然要加后者啊，加群申请就很有感觉：目前哪吒的实时票房是：，我去到猫眼专业版，背下那串数字，输入答案。</p>
<p>进了群，就是几位群友在发欢迎新人，我也回了句“谢谢大家”，作为名事业粉，在打完招呼后，我就开始讨论起票房来。群里很和睦，有时候还对对台词，他们把这种活动叫做“踢毽子”。</p>
<p>中秋时候吒儿的战斗力依然很强，虽然排片不占优势，但是却上了2000w日票房，群里把破千票房叫“过小年”，过大年自然就是破亿了。</p>
<p>中秋，我和朋友约好去看吒儿的，这是我第7刷。</p>
<h2 id="以后的计划"><a href="#以后的计划" class="headerlink" title="以后的计划"></a>以后的计划</h2><p>目前为止，哪吒依然是我最喜欢的一个角色，在几年内估计也是这样，目前吒儿的微博虽然不像当时上映期间那么活跃，但也是隔几天发一条微博，基本很少有隔10天以上的。</p>
<p>总之，哪吒我也许会一直关注下去。就像我对待爱情一样（虽然我没有过…），哪吒只要还记得我们这群粉丝，我也就不会忘记他。</p>
<p>Python for Nezha<br>这是一个项目，设计一个哪吒风格的Python，目前只完成了shell的美化。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/02/%E5%81%9A%E8%97%95%E7%B2%89%E7%9A%84%E7%AC%AC236%E5%A4%A9/" data-id="ck8swb2zz0005wds65m803qk4" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%B5%81%E6%B0%B4%E8%B4%A6/" rel="tag">流水账</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="post-SAIL开发日志-1" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/04/01/SAIL%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97-1/" class="article-date">
  <time datetime="2020-04-01T01:00:00.000Z" itemprop="datePublished">2020-04-01</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/04/01/SAIL%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97-1/">SAIL开发日志 (1)</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>今天应该是SAIL开发的第20天，也是我第一次写博客，markdown还不太会用，嘻嘻。</p>
<p>既然是第一篇关于SAIL的文章，我还是讲讲SAIL是啥吧。<br>SAIL是一门编程语言，介绍完毕。</p>
<h2 id="SAIL的向往"><a href="#SAIL的向往" class="headerlink" title="SAIL的向往"></a>SAIL的向往</h2><p><code>SAIL</code>是风帆的意思，SAIL 旨意让开发者既能像python那样，有各式各样的库和模块，也能像java那样，有这严谨的面向对象的编程范式。</p>
<p><strong>可以说，SAIL是由一位热爱着 Python 的程序员设计出来的 Java</strong></p>
<p>来看看 SAIL Java Python 它们各自面向对象的<code>Hello World</code>实现</p>
<p>下面是 SAIL 编写的 Hello World 程序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Hello</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="title">main</span><span class="params">(args []String)</span> </span>&#123;</span><br><span class="line">        Console.writeln(<span class="string">"Hello World"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>（我不会告诉你我用了Java的高亮方案（狗头保命））</p>
<p>下面是 Java 的 Hello World 程序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Hello</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"Hello World"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看起来差不多的亚子，来看看Python的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Hello</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">(self, *args)</span>:</span></span><br><span class="line">        print(<span class="string">"Hello World"</span>)</span><br></pre></td></tr></table></figure>
<p>Python所需要的字符是最少的，Java是最多的，SAIL中规中矩</p>
<h2 id="在Java的基础上做精简"><a href="#在Java的基础上做精简" class="headerlink" title="在Java的基础上做精简"></a>在Java的基础上做精简</h2><p><strong><em>会Java的程序员，我敢打包票会写SAIL，因为他俩太像了</em></strong></p>
<p>SAIL 尽可能的做精简，有些东西不需要特殊表明的，就省下，例如<code>void</code>，尽管你可以显式指出<code>void</code>返回值。</p>
<p>当然，还有<code>var</code>语句<code>var a = 10;</code>，可以选择性地加上类型，若不给出类型，编译期会补上。</p>
<h2 id="一-new-多用"><a href="#一-new-多用" class="headerlink" title="一 new 多用"></a>一 new 多用</h2><p>new 是SAIL较为有趣的一个地方，虽然它的功能只有一个，就是实例化对象，但是用起来相比Java的 new ，体验上会有一点区别。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> override Runnable &#123;</span><br><span class="line">    <span class="keyword">while</span> socs.accept() &#123;</span><br><span class="line">        <span class="comment">// ...</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;.run()</span><br></pre></td></tr></table></figure>
<p>这是快速创建一个线程的方式，也是快速重载只有一个public方法的class或者interface的高效方法。（Kotlin在这一点做得很好）</p>
<h2 id="类型后置"><a href="#类型后置" class="headerlink" title="类型后置"></a>类型后置</h2><p><strong><em>为什么 SAIL 会选择“奇怪”的类型后置语法？</em></strong></p>
<p>理由很简单：<strong>好看，易读</strong></p>
<p>一段代码，若使用类型后置，读起来将会很顺口。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">public getAge() int;</span><br></pre></td></tr></table></figure>

<h2 id="收尾"><a href="#收尾" class="headerlink" title="收尾"></a>收尾</h2><p>说实话，我写作文最怕收尾了。<br>SAIL 作为一门编程语言，为开发者着想，特别是为开发者的眼睛着想。</p>
<blockquote>
<p>*一个注重代码简洁和美丽的人，设计出来的语言，估计也不会差到哪去。</p>
</blockquote>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/04/01/SAIL%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97-1/" data-id="ck8swb2yt0000wds608mf8d6x" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SAIL%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/" rel="tag">SAIL开发日志</a></li></ul>

    </footer>
  </div>
  
</article>


  


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/SAIL%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/" rel="tag">SAIL开发日志</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python%E6%95%99%E7%A8%8B/" rel="tag">python教程</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E5%93%AA%E5%90%92%E7%9B%B8%E5%85%B3/" rel="tag">哪吒相关</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%B5%81%E6%B0%B4%E8%B4%A6/" rel="tag">流水账</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%8A%B1%E9%87%8C%E8%83%A1%E5%93%A8/" rel="tag">花里胡哨</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/SAIL%E5%BC%80%E5%8F%91%E6%97%A5%E5%BF%97/" style="font-size: 10px;">SAIL开发日志</a> <a href="/tags/python%E6%95%99%E7%A8%8B/" style="font-size: 20px;">python教程</a> <a href="/tags/%E5%93%AA%E5%90%92%E7%9B%B8%E5%85%B3/" style="font-size: 10px;">哪吒相关</a> <a href="/tags/%E6%B5%81%E6%B0%B4%E8%B4%A6/" style="font-size: 15px;">流水账</a> <a href="/tags/%E8%8A%B1%E9%87%8C%E8%83%A1%E5%93%A8/" style="font-size: 10px;">花里胡哨</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">April 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/04/05/%E3%80%90LaTeX%E6%B5%8B%E8%AF%95%E3%80%91%E4%B8%80%E4%BA%9B%E8%91%97%E5%90%8D%E7%9A%84%E5%85%AC%E5%BC%8F/">【LaTeX测试】一些著名的公式</a>
          </li>
        
          <li>
            <a href="/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%89%EF%BC%89%E5%AE%9E%E7%8E%B0property%E7%B1%BB/">搞定Python装饰器（三）实现property类</a>
          </li>
        
          <li>
            <a href="/2020/04/04/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%BA%8C%EF%BC%89/">搞定Python装饰器（二）</a>
          </li>
        
          <li>
            <a href="/2020/04/04/%E3%80%90%E5%9B%BE%E7%89%87%E6%B5%8B%E8%AF%95%E3%80%91%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%92%E5%84%BF/">【图片测试】可爱的吒儿</a>
          </li>
        
          <li>
            <a href="/2020/04/03/%E6%90%9E%E5%AE%9APython%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E4%B8%80%EF%BC%89/">搞定 Python 装饰器（一）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 Laomo.<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>